/**
 * 
 */
package com.appspot.myjsf2.data.webaccess;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.myjsf2.data.TestDAO;
import com.appspot.myjsf2.data.entity.MyTestDataBean;

/**
 * @author andyz
 *
 */
public class AccessTestDataServlet extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		resp.setContentType("text/plain");
		System.out.println("getting data!");
		PrintWriter writer = resp.getWriter();
		if(req.getParameter("method").equalsIgnoreCase("create")){
			String name = req.getParameter("name");
			String content = req.getParameter("content");
			MyTestDataBean testData = new TestDAO().createTestData(name, content);
			resp.getWriter().println("sucess , id: " + testData.getId());
		}
		if(req.getParameter("method").equalsIgnoreCase("get")){
			Long id = Long.parseLong(req.getParameter("id"));
			MyTestDataBean testData = new TestDAO().getTestData(id);
			writer.print("name: " + testData.getName());
			writer.print("content: " + testData.getContent());
		}
		resp.getWriter().println("done!");
	}
}
